﻿using System;
using System.Collections.Generic;
using System.Linq;
using Ext.Net;

namespace WEB.Core.Controls
{
    public partial class BasePage
    {
        #region
        private List<Component> _ListControlAdd = new List<Component>();
        private List<Component> _ListControlEdit = new List<Component>();
        private List<Component> _ListControlPrint = new List<Component>();
        private List<Component> _ListControlDelete = new List<Component>();
        private List<Component> _ListControlImport = new List<Component>();
        private List<Component> _ListControlExport = new List<Component>();
        private List<Component> _ListControlNotDashboard = new List<Component>();
        #endregion

        #region
        protected ResourceManager ResourceManager
        {
            get { return (ResourceManager)FindControl("ResourceManager"); }
        }
        protected List<Component> ListControlAdd
        {
            get { return _ListControlAdd; }
            set { _ListControlAdd = value; }
        }
        protected List<Component> ListControlEdit
        {
            get { return _ListControlEdit; }
            set { _ListControlEdit = value; }
        }
        protected List<Component> ListControlDelete
        {
            get { return _ListControlDelete; }
            set { _ListControlDelete = value; }
        }
        protected List<Component> ListControlPrint
        {
            get { return _ListControlPrint; }
            set { _ListControlPrint = value; }
        }
        protected List<Component> ListControlImport
        {
            get { return _ListControlImport; }
            set { _ListControlImport = value; }
        }
        protected List<Component> ListControlExport
        {
            get { return _ListControlExport; }
            set { _ListControlExport = value; }
        }
        protected List<Component> ListControlNotDashboard
        {
            get { return _ListControlNotDashboard; }
            set { _ListControlNotDashboard = value; }
        }
        #endregion

        #region AddListControl
        private static void AddListControl(ref List<Component> ListControl, params Component[] ListControlAdd)
        {
            foreach (var item in ListControlAdd)
            {
                ListControl.Add(item);
            }
        }
        protected void AddListControlAdd(params Component[] ListControl)
        {
            AddListControl(ref _ListControlAdd, ListControl);
        }
        protected void AddListControlEdit(params Component[] ListControl)
        {
            AddListControl(ref _ListControlEdit, ListControl);
        }
        protected void AddListControlDelete(params Component[] ListControl)
        {
            AddListControl(ref _ListControlDelete, ListControl);
        }
        protected void AddListControlPrint(params Component[] ListControl)
        {
            AddListControl(ref _ListControlDelete, ListControl);
        }
        protected void AddListControlImport(params Component[] ListControl)
        {
            AddListControl(ref _ListControlImport, ListControl);
        }
        protected void AddListControlExport(params Component[] ListControl)
        {
            AddListControl(ref _ListControlExport, ListControl);
        }
        protected void AddListControlNotDashboard(params Component[] ListControl)
        {
            AddListControl(ref _ListControlNotDashboard, ListControl);
        }
        #endregion


        #region

        #region Script
        /// <summary>
        /// Thêm file JavaScript vào Header
        /// </summary>
        /// <param name="Path">Đường dẫn file JavaScript (không có "/Javascript/")</param>
        protected void AddScript(string Path)
        {
            System.Web.UI.HtmlControls.HtmlGenericControl _Script = new System.Web.UI.HtmlControls.HtmlGenericControl("script");
            _Script.Attributes.Add("type", "text/javascript");
            _Script.Attributes.Add("src", "/Javascript/" + Path);
            Header.Controls.Add(_Script);
        }

        /// <summary>
        /// Thêm nhiều file JavaScript vào Header
        /// </summary>
        /// <param name="Paths">Đường dẫn file JavaScript (không có "/Javascript/")</param>
        protected void AddScript(params string[] Paths)
        {
            foreach (var item in Paths)
            {
                AddScript(item);
            }
        }

        /// <summary>
        /// Khởi tạo các file JavaScript vào Header
        /// </summary>
        protected virtual void AddPageScript()
        {
            AddScript("Global.js");
        }
        #endregion

        protected virtual void CreateResourceManager(bool HaveDocumentReady)
        {
            ResourceManager _ResourceManager = new ResourceManager()
            {
                ID = "ResourceManager",
                DirectMethodNamespace = "MethodNamespace"
            };
            if (HaveDocumentReady)
            {
                _ResourceManager.Listeners.DocumentReady.Handler = "DocumentReady();";
                _ResourceManager.Listeners.DocumentReady.Delay = 100;
            }
            Page.Controls.AddAt(0, _ResourceManager);
        }

        /// <summary>
        /// Kiểm tra và thay đổi nếu trang ở Dashboard
        /// </summary>
        protected virtual void CheckDashboard()
        {
            if (!_CheckDashboard)
                return;

            if (IsDashboard)
                _ListControlNotDashboard.All(control => control.Hidden = IsDashboard);
            //foreach (var item in _ListControlNotDashboard)
            //    item.Hidden = IsDashboard;
        }


        protected Hidden GetHidden(string ID)
        {
            var _Control = (Hidden)FindControl(ID);
            if (null == _Control)
            {
                _Control = new Hidden() { ID = ID };
                this.Controls.Add(_Control);
            }
            return _Control;
        }
        #endregion
    }
}
